package com.hanhanxiaochong.zhongxiangchuanmei.view.main.act;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.BannerBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.MemberShipBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.SystemNoticeBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.HomePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.GlideImageLoader;
import com.hanhanxiaochong.zhongxiangchuanmei.util.MessageEvent;
import com.hanhanxiaochong.zhongxiangchuanmei.util.Utils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseLayout;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.HomeVipActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.HotProjectActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.HotProjectDetailActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.MyGiftsActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.NearbyShopActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.SearchActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IHomeView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MsgActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.AutoScrollTextView;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/5.
 * Desc   :
 */

public class HomeLayout extends BaseLayout<HomePresenter> implements IHomeView {

    @BindView(R.id.txt_city)
    TextView txtCity;
    @BindView(R.id.home_banner)
    Banner homeBanner;
    @BindView(R.id.et_home_search)
    EditText etHomeSearch;
    @BindView(R.id.scrollnews)
    AutoScrollTextView scrollnews;
    @BindView(R.id.iv_hot1)
    ImageView ivHot1;
    @BindView(R.id.iv_hot2)
    ImageView ivHot2;
    @BindView(R.id.iv_hot3)
    ImageView ivHot3;
    @BindView(R.id.iv_hot4)
    ImageView ivHot4;
    @BindView(R.id.iv_vip1)
    ImageView ivVip1;
    @BindView(R.id.iv_vip2)
    ImageView ivVip2;
    @BindView(R.id.iv_vip3)
    ImageView ivVip3;
    @BindView(R.id.iv_sell1)
    ImageView ivSell1;
    @BindView(R.id.iv_sell2)
    ImageView ivSell2;
    @BindView(R.id.iv_sell3)
    ImageView ivSell3;

    private ArrayList<String> bannerLinks;
    private List<MemberShipBean.HotBean> hot;
    private List<MemberShipBean.HotBean> list_member;
    private List<MemberShipBean.HotBean> selling;

    public HomeLayout(Context context) {
        super(context);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_home;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new HomePresenter(this);
    }

    @Override
    protected void init() {
        etHomeSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String keyword = etHomeSearch.getText().toString().trim();
                mContext.startActivity(SearchActivity.createIntent(mContext, keyword));
                 /*隐藏软键盘*/
                InputMethodManager inputMethodManager = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager.isActive()) {
                    inputMethodManager.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), 0);
                }
            }
            return false;
        });

        mPresenter.getBanner();
        mPresenter.getSystemNews();
        mPresenter.getHot();
        //动态设置轮播图高度(4:3)
        ViewGroup.LayoutParams layoutParams = homeBanner.getLayoutParams();
        layoutParams.height = (int) (Utils.getScreenWidth(mContext) * 0.5333333);
        homeBanner.requestLayout();
        //设置banner样式
        homeBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        homeBanner.setImageLoader(new GlideImageLoader());
        //设置banner动画效果
        homeBanner.setBannerAnimation(Transformer.ZoomOut);
        //设置自动轮播，默认为true
        homeBanner.isAutoPlay(true);
        //设置轮播时间
        homeBanner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        homeBanner.setIndicatorGravity(BannerConfig.CENTER);
        //下标是从1开始，同时解决下标越界问题
        homeBanner.setOnBannerListener(position -> {

        });
    }

    @OnClick({R.id.txt_city, R.id.img_home_msg, R.id.ll_home_announcement, R.id.ll_home_vip, R.id
            .ll_home_nearby_shop, R.id.ll_home_hotproject, R.id.ll_home_mygifts, R.id.iv_hot1,
            R.id.iv_hot2, R.id.iv_hot3, R.id.iv_hot4, R.id.iv_vip1, R.id.iv_vip2, R.id.iv_vip3,
            R.id.iv_sell1, R.id.iv_sell2, R.id.iv_sell3,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_city:
                mContext.startActivity(ChooseCityActivity.createIntent(mContext));
                break;
            case R.id.img_home_msg:
                Intent intent = new Intent(mContext, MsgActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.ll_home_announcement:
                Intent intent1 = new Intent(mContext, MsgActivity.class);
                mContext.startActivity(intent1);
                break;
            case R.id.ll_home_vip:
                Intent intent2 = new Intent(mContext, HomeVipActivity.class);
                mContext.startActivity(intent2);
                break;
            case R.id.ll_home_nearby_shop:
                Intent intent3 = new Intent(mContext, NearbyShopActivity.class);
                mContext.startActivity(intent3);
                break;
            case R.id.ll_home_hotproject:
                Intent intent5 = new Intent(mContext, HotProjectActivity.class);
                mContext.startActivity(intent5);
                break;
            case R.id.ll_home_mygifts:
                Intent intent4 = new Intent(mContext, MyGiftsActivity.class);
                mContext.startActivity(intent4);
                break;
            case R.id.iv_hot1:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, hot.get(0).getId()));
                break;
            case R.id.iv_hot2:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, hot.get(1).getId()));
                break;
            case R.id.iv_hot3:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, hot.get(2).getId()));
                break;
            case R.id.iv_hot4:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, hot.get(3).getId()));
                break;
            case R.id.iv_vip1:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, list_member.get(0).getId()));
                break;
            case R.id.iv_vip2:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, list_member.get(1).getId()));
                break;
            case R.id.iv_vip3:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, list_member.get(2).getId()));
                break;
            case R.id.iv_sell1:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, selling.get(0).getId()));
                break;
            case R.id.iv_sell2:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, hot.get(1).getId()));
                break;
            case R.id.iv_sell3:
                mContext.startActivity(HotProjectDetailActivity.createIntent(mContext, hot.get(2).getId()));
                break;
        }
    }


    @Override
    public void setBanner(List<BannerBean> data) {
        ArrayList<String> imgs = new ArrayList<>();
        bannerLinks = new ArrayList<>();
        for (BannerBean bannerBean :
                data) {
            imgs.add(RetrofitSource.image_url + bannerBean.getSlide_pic());
            bannerLinks.add(bannerBean.getSlide_url());
        }
        homeBanner.setImages(imgs);
        homeBanner.start();
    }

    @Override
    public void setSystemNews(List<SystemNoticeBean> titles) {
        List<String> contents = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++)
            contents.add(titles.get(i).getContent());

        scrollnews.setTexts(contents);
        scrollnews.setContentTextColor(Color.BLACK);
        scrollnews.start();
    }

    @Override
    public void setHot(MemberShipBean data) {
        hot = data.getHot();
        list_member = data.getList_member();
        selling = data.getSelling();
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + hot.get(0).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivHot1);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + hot.get(1).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivHot2);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + hot.get(2).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivHot3);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + hot.get(3).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivHot4);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + list_member.get(2).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivVip1);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + list_member.get(1).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivVip2);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + list_member.get(0).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivVip3);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + selling.get(0).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivSell1);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + selling.get(1).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivSell2);
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + selling.get(2).getPic(), R.mipmap.img_error, R.mipmap.img_error, ivSell3);

        int screenWidth = Utils.getScreenWidth(mContext);
        ViewGroup.LayoutParams ivHot1LayoutParams = ivHot1.getLayoutParams();
        ivHot1LayoutParams.width = (int) (screenWidth * 0.5);
        ivHot1LayoutParams.height = (int) (ivHot1LayoutParams.width * 0.4783);
        ivHot1.setLayoutParams(ivHot1LayoutParams);
        ViewGroup.LayoutParams ivHot2LayoutParams = ivHot2.getLayoutParams();
        ivHot2LayoutParams.width = (int) (screenWidth * 0.5);
        ivHot2LayoutParams.height = (int) (ivHot2LayoutParams.width * 0.4783);
        ivHot2.setLayoutParams(ivHot2LayoutParams);
        ViewGroup.LayoutParams ivHot3LayoutParams = ivHot3.getLayoutParams();
        ivHot3LayoutParams.width = (int) (screenWidth * 0.5);
        ivHot3LayoutParams.height = (int) (ivHot3LayoutParams.width * 0.4783);
        ivHot3.setLayoutParams(ivHot3LayoutParams);
        ViewGroup.LayoutParams ivHot4LayoutParams = ivHot4.getLayoutParams();
        ivHot4LayoutParams.width = (int) (screenWidth * 0.5);
        ivHot4LayoutParams.height = (int) (ivHot4LayoutParams.width * 0.4783);
        ivHot4.setLayoutParams(ivHot4LayoutParams);
        ViewGroup.LayoutParams ivVip1LayoutParams = ivVip1.getLayoutParams();
        ivVip1LayoutParams.width = (int) (screenWidth * 0.4071);
        ivVip1LayoutParams.height = (int) (ivVip1LayoutParams.width * 1.254);
        ivVip1.setLayoutParams(ivVip1LayoutParams);
        ViewGroup.LayoutParams ivVip2LayoutParams = ivVip2.getLayoutParams();
        ivVip2LayoutParams.width = (int) (screenWidth * 0.5428);
        ivVip2LayoutParams.height = (int) (ivVip2LayoutParams.width * 0.4643);
        ivVip2.setLayoutParams(ivVip2LayoutParams);
        ViewGroup.LayoutParams ivVip3LayoutParams = ivVip3.getLayoutParams();
        ivVip3LayoutParams.width = (int) (screenWidth * 0.5428);
        ivVip3LayoutParams.height = (int) (ivVip3LayoutParams.width * 0.4643);
        ivVip3.setLayoutParams(ivVip3LayoutParams);
        ViewGroup.LayoutParams ivSell1LayoutParams = ivSell1.getLayoutParams();
        ivSell1LayoutParams.width = (int) (screenWidth * 0.5428);
        ivSell1LayoutParams.height = (int) (ivSell1LayoutParams.width * 0.4643);
        ivSell1.setLayoutParams(ivSell1LayoutParams);
        ViewGroup.LayoutParams ivSell2LayoutParams = ivSell2.getLayoutParams();
        ivSell2LayoutParams.width = (int) (screenWidth * 0.5428);
        ivSell2LayoutParams.height = (int) (ivSell2LayoutParams.width * 0.4643);
        ivSell2.setLayoutParams(ivSell2LayoutParams);
        ViewGroup.LayoutParams ivSell3LayoutParams = ivSell3.getLayoutParams();
        ivSell3LayoutParams.width = (int) (screenWidth * 0.4071);
        ivSell3LayoutParams.height = (int) (ivSell3LayoutParams.width * 1.254);
        ivSell3.setLayoutParams(ivSell3LayoutParams);
    }

    public void setCity(String city) {
        txtCity.setText(city);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleEvent(MessageEvent event) {
        if (event != null && event.getFrom().equals("city"))
            txtCity.setText(event.getMessage());
    }

}
